//
//  LGFollowRecordController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/10/7.
//  随访记录

#import "LGFollowRecordController.h"

#import "LGMedicineBallFollowUpRecorder.h"

#import "LGFollowRecordHeaderView.h"
#import "LGFollowRecordNormalCell.h"
#import "LGFollowRecordCell.h"
#import "LGCommonScreenView.h"
#import "LGFollowRecordEditAlert.h"
@interface LGFollowRecordController ()<UITableViewDelegate,UITableViewDataSource,LGFollowRecorHeaderDelegate,LGBallDetailFollowRecordDelegate,LGFollowRecordEditAlertDelegate>
/**   */
@property (nonatomic , strong)LGFollowRecordHeaderView *headerView;
/**   */
@property (nonatomic , strong)LGCommonScreenView *screenView;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)NSArray *dataArray;
/**  筛选条件 */
@property (nonatomic , strong)NSMutableDictionary *screenDic;

@end

@implementation LGFollowRecordController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor clearColor];
    self.levelid = (_levelid && _levelid.length>0)?_levelid:@"";
    [self.view addSubview:self.tableView];
    [self emptyViewWithSuperView:self.tableView frame:CGRectMake(0, kMaxY(self.headerView.frame), Screen_W, self.tableView.bounds.size.height-kMaxY(self.headerView.frame))];
    self.emptyView.backgroundColor = [UIColor clearColor];
    [[UIApplication sharedApplication].keyWindow addSubview:self.screenView];
    [self requestScreenData];
}

-(void)requestScreenData{
    [RequestUtil withPOST:@"/doctor/patient.followup_advice/filter" parameters:@{@"patient_id":self.patientid,@"level_id":self.levelid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            self.screenView.itemArray = returnData;
            [self sureScreenAction];
        }
    } error:^(NSError *error) {
        
    }];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/patient.followup_advice/search" parameters:@{@"patient_id":self.patientid,@"filter_json":[ObjectTool objectToJson:self.screenDic],@"level_id":self.levelid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if(code == 200){
            self.dataArray = returnData;
            self.headerView.totalCount = [NSString stringWithFormat:@"%ld",self.dataArray.count];
        }
        [self.tableView reloadData];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无随访记录" offset:0];
        }
    } error:^(NSError *error) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无随访记录" offset:0];
        }
    }];
}

//筛选
-(void)screenBtnAction{
    [self.screenView showScreenViewAnimation];
}

//重置筛选
-(void)resetScreenAction{
    self.screenDic = [NSMutableDictionary dictionary];
    [self.screenView resetDataAction];
    [self.screenView closeViewAction];
    self.headerView.dataDic = @{};
    [self requestData];
}

//删除筛查选项
-(void)deleteScreenItem:(NSDictionary *)itemDic{
    NSString *key = LGNSString(itemDic[@"key"]);
    NSString *content = LGNSString(itemDic[@"title"]);
    if([self.screenView.screenDic.allKeys containsObject:key]){
        if([key containsString:@"_start"] || [key containsString:@"_end"]){
            [self.screenView.screenDic removeObjectForKey:key];
        }else{
            NSMutableDictionary *tempDic = [NSMutableDictionary dictionaryWithDictionary:self.screenView.screenDic[key]];
            if([tempDic.allKeys containsObject:content]){
                [tempDic removeObjectForKey:content];
            }
            [self.screenView.screenDic setValue:tempDic forKey:key];
        }
    }
    [self.screenView.collectionView reloadData];
    [self sureScreenAction];
}

//确定筛选
-(void)sureScreenAction{
    self.screenDic = [NSMutableDictionary dictionary];
    NSArray *keyArray = self.screenView.screenDic.allKeys;
    for (NSInteger i=0; i<keyArray.count; i++) {
        NSString *key = keyArray[i];
        if([key containsString:@"_start"] || [key containsString:@"_end"]){
            [self.screenDic setValue:self.screenView.screenDic[key] forKey:key];
        }else{
            NSDictionary *tempDic = self.screenView.screenDic[key];
            NSArray *titleArray = tempDic.allKeys;
            if(titleArray.count>0){
                NSMutableArray *tempArray = [NSMutableArray array];
                for (NSInteger j=0; j<titleArray.count; j++) {
                    [tempArray addObject:@{@"title":titleArray[j],@"ids":tempDic[titleArray[j]]}];
                }
                [self.screenDic setValue:tempArray forKey:keyArray[i]];
            }
        }
    }
    self.headerView.dataDic = self.screenView.screenDic;
    [self.screenView closeViewAction];
    [self requestData];
}

//修改随访总结
-(void)reviseFollowUpRecord:(NSString *)followid{
    LGFollowRecordEditAlert *alertView = [[LGFollowRecordEditAlert alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    alertView.followid = followid;
    alertView.delegate = self;
    [[UIApplication sharedApplication].keyWindow addSubview:alertView];
    
}

//修改随访总结
-(void)editFollowRecord:(NSString *)followid{
    LGMedicineBallFollowUpRecorder *controller = [[LGMedicineBallFollowUpRecorder alloc]init];
    controller.followid = followid;
    [self.navigationController pushViewController:controller animated:YES];
    __weak typeof(self) weakSelf = self;
    controller.opinionSaveSuccess = ^(NSDictionary * _Nonnull dataDic) {
        [weakSelf requestData];
    };
}

//删除随访总结
-(void)deleteFollowRecord:(NSString *)followid{
    __block NSString *followId = followid;
    __weak typeof(self) weakSelf = self;
    [LGAlertTool alertWithTitle:@"删除随访总结" content:@"确定删除这项随访总结？" sureAction:^{
        [RequestUtil withPOST:@"/doctor/patient.dcb.follow_up/delete" parameters:@{@"ids":followid,@"level_id":self.levelid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
            if(code == 200){
                [weakSelf requestData];
            }
        } error:^(NSError *error) {
            
        }];
    }];
}

-(void)headViewHeightChanged:(CGFloat)height{
    self.headerView.frame = CGRectMake(0, 0, Screen_W, height);
    self.tableView.tableHeaderView = self.headerView;
    self.emptyView.frame = CGRectMake(0, height, Screen_W, self.tableView.bounds.size.height-height);
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *indexType;
    if(self.dataArray.count == 1){
        indexType = @"single";
    }else if (indexPath.row == 0){
        indexType = @"top";
    }else if (indexPath.row == self.dataArray.count-1){
        indexType = @"bottom";
    }else{
        indexType = @"middle";
    }
    
    NSDictionary *tempDic = self.dataArray[indexPath.row];
    //1-电话随访 、 2-短信随访 、 3-随访总结 、 4-计划调整
    NSString *type = LGNSString(tempDic[@"followup_type"]);
    if([type isEqualToString:@"3"]){
        //随访记录 -- 随访总结
        LGFollowRecordCell *recordCell = [tableView dequeueReusableCellWithIdentifier:@"recordCell" forIndexPath:indexPath];
        recordCell.selectionStyle = UITableViewCellSelectionStyleNone;
        recordCell.dataDic = tempDic;
        recordCell.type = indexType;
        recordCell.delegate = self;
        return recordCell;

    }else{
        //随访记录 -- noramal
        LGFollowRecordNormalCell *normalCell = [tableView dequeueReusableCellWithIdentifier:@"normalCell" forIndexPath:indexPath];
        normalCell.selectionStyle = UITableViewCellSelectionStyleNone;
        normalCell.dataDic = tempDic;
        normalCell.type = indexType;
        return normalCell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, 0.01)];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.001;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, 0.01)];
}


#pragma mark
#pragma mark ====> 创建控件
-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    self.tableView.frame = CGRectMake(0, 0, Screen_W, self.view.bounds.size.height);
//    self.emptyView.frame = CGRectMake(0, 0, Screen_W, self.view.bounds.size.height);
}

-(NSMutableDictionary *)screenDic{
    if(!_screenDic){
        _screenDic = [NSMutableDictionary dictionary];
    }
    return _screenDic;
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight-viewPix(44)) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGFollowRecordNormalCell class] forCellReuseIdentifier:@"normalCell"];
        [_tableView registerClass:[LGFollowRecordCell class] forCellReuseIdentifier:@"recordCell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.estimatedRowHeight = viewPix(46);
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = self.headerView;
    }
    return _tableView;
}
-(LGFollowRecordHeaderView *)headerView{
    if(!_headerView){
        _headerView = [[LGFollowRecordHeaderView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(44))];
        _headerView.delegate = self;
        [_headerView.screenBtn addTarget:self action:@selector(screenBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _headerView;
}

-(LGCommonScreenView *)screenView{
    if(!_screenView){
        _screenView = [[LGCommonScreenView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
        [_screenView.resetBtn addTarget:self action:@selector(resetScreenAction) forControlEvents:UIControlEventTouchUpInside];
        [_screenView.sureBtn addTarget:self action:@selector(sureScreenAction) forControlEvents:UIControlEventTouchUpInside];
        _screenView.hidden = YES;
    }
    return _screenView;
}
@end
